package com.situ.springboot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.springboot.entity.Student;
import com.situ.springboot.mapper.StudentMapper;
import com.situ.springboot.service.IStudentService;
import com.situ.springboot.service.IUserService;
import com.situ.springboot.util.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Service
@Transactional
public class StudentServiceImpl implements IStudentService {
    private static final Logger logger = LoggerFactory.getLogger(StudentServiceImpl.class);

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private IUserService userService;

    @Override
    public List<Student> selectAll() {
        return studentMapper.selectAll();
    }

    /*@Override
    public PageInfo selectByPage(Integer pageNo, Integer pageSize) {
        System.out.println("StudentServiceImpl.selectByPage");
        int offset = (pageNo - 1) * pageSize;
        ArrayList<Student> list = studentMapper.selectByPage(offset, pageSize);
        int totalCount = studentMapper.selectTotalCount();
        // 向上取整，算出总的页数
        int totalPage = (int)Math.ceil((double)totalCount / pageSize);
        PageInfo pageInfo = new PageInfo(list, pageNo, totalPage, pageSize);
        System.out.println(pageInfo);
        return pageInfo;
    }*/

    @Override
    public PageBean selectByPage(Integer pageNo, Integer pageSize) {
        System.out.println("StudentServiceImpl.selectByPage");
        // 记录开始时间
        //long begin = System.currentTimeMillis();

        PageHelper.startPage(pageNo, pageSize);
        ArrayList<Student> list = studentMapper.selectByPage();
        //用PageInfo对结果进行包装
        PageInfo pageInfo = new PageInfo(list);
        long totalCount = pageInfo.getTotal();
        // 向上取整，算出总的页数
        int totalPage = (int)Math.ceil((double)totalCount / pageSize);
        PageBean pageBean = new PageBean(list, pageNo, totalPage, pageSize);
        System.out.println(pageInfo);

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 记录结束时间
        /*long end = System.currentTimeMillis();
        long takeTime = end - begin;
        if (takeTime >= 3000) {
            logger.error("=========执行结束，耗时：{} 毫秒=========", takeTime);
        } else if (takeTime >= 2000) {
            logger.warn("=========执行结束，耗时：{} 毫秒=========", takeTime);
        } else {
            logger.info("=========执行结束，耗时：{} 毫秒=========", takeTime);
        }*/

        return pageBean;
    }

    // Transaction
    // propagation:事物的传播属性（特性、级别）
    // isolation:事物的隔离级别
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    @Override
    public Student selectById(Integer id) {
        System.out.println("StudentServiceImpl.selectById");
        Student student1 = studentMapper.selectById(id);
        System.out.println(student1);
        Student student2 = studentMapper.selectById(id);
        System.out.println(student2);

        // deleteById(23);
        return null;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void deleteById(Integer id) {
        System.out.println("StudentServiceImpl.deleteById");
    }
}
